//-------------------------------------------------------------------------------
//	file p_mem.cpp
//	brief
//
//	$Id: p_mem.c 276 2014-04-17 07:34:45Z ac.verbeck@gmail.com $
//------------------------------------------------------------------------------
//------------------------------------------------------------------------------
//	Copyright and Disclaimer Notice
//
//	Copyright (c) 2013-2014 A.C. Verbeck
//
//	This file is part of Shruthi Patch Reader (SPR)
//
//	Shruthi Patch Reader (SPR) is free software: you can redistribute it and/or modify
//	it under the terms of the GNU General Public License as published by
//	the Free Software Foundation, either version 3 of the License, or
//	(at your option) any later version.
//
//	Shruthi Patch Reader (SPR) is distributed in the hope that it will be useful,
//	but WITHOUT ANY WARRANTY; without even the implied warranty of
//	MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
//	GNU General Public License for more details.
//
//	You should have received a copy of the GNU General Public License
//	along with Shruthi Patch Reader (SPR).
//	If not, see <http://www.gnu.org/licenses/>.
//
//------------------------------------------------------------------------------

#include <stdio.h>
#include <string.h>
#include <stdint.h>

#include "p_mem.h"

void p_mem(uint8_t* buff, uint32_t length)
{
	uint32_t row, col, i=0;														//	Declare row, col, and i.  Set i to 0

	printf("      00 01 02 03 04 05 06 07 08 09 0a 0b 0c 0d 0e 0f\n");
	for (row=0; (i+1)<length; row++) {											//	Iterate through 16 byte wide rows
		printf("%04x: ",row*16);
		for (col=0; col<16; col++) {											//	Print hex
			i = row*16+col;														//	Calculate current position
			if (i<length)														//	Print hex value if the current index is in range
				printf("%02x", buff[i]);
			else																//	Print a blank if the current index is past the end
				printf("  ");
			printf(" ");														//	Print a space to keep alignment
		}
		printf("| ");															//	Add a vertical separator between hex and ASCII representations

		for (col=0; col<16; col++) {											//	Print ASCII representation
			i = row*16+col;														//	Calculate the current index
			if (i<length) {														//	Print the value if it is in range
				if (buff[i]>0x20 && buff[i]<0x7F) 								//	Print the ASCII value if applicable
					printf("%c", buff[i]);
				else															//	Else print a dot
					printf(".");
			} else																//	Nothing else to print, so break out of this for loop
				break;
		}
		printf("\n");															//	Goto next line
	}
}

//
//	End: p_mem.cpp
//
